﻿
/******************************************************************************************************************************
 **********************************************************/
var cachePoolHelper = (function (window) {
    /*
    * IE 兼容
    */
    if (!Date.now) {
        Date.now = function now() {
            return new Date().getTime();
        };
    }
    var cachePool = {
        limitation: 1000//时效，毫秒
        /*
        * 添加到缓存池 
        * @param limitation 自定义优先级最高的时效
        */
        , add: function add(key, value, limitation) {
            this.__pool.objects[key] = value;
            //加时间戳  每次刷新时间戳 key不存在，或者没有timespan
            if (!this.__pool.objects[key] || !this.__pool.timeSpans[key])//一个缓存对象只创建一个时间戳对象。
                this.__pool.timeSpans[key] = new TimeSpan(limitation || this.limitation, !limitation);
            else
                this.__pool.timeSpans[key].init(limitation || this.limitation, !limitation);//刷新isown属性
        }
        /*
        * 移除缓存 
        */
        , remove: function remove(key) {
            return (delete this.__pool.objects[key]) && (delete this.__pool.timeSpans[key]);
        }
        , get: function get(key) {
            if (this.__pool.objects[key]&&this.__pool.timeSpans[key].getIsActive())
                return this.__pool.objects[key];
            else
                return null;
        }
        /**
        * 如果设置了自己的时效，isOwn属性将为true，在重新设置全局对象的时候，可以选择不刷新优先级最高的。
        */
        , setLimitation: function (limitation, freshOwn) {
            this.limitation = limitation;
            for (var i in this.__pool.timeSpans) {
                if (freshOwn)
                    this.__pool.timeSpans[i].init(limitation);
                else if (!this.__pool.timeSpans[i].isOwn)
                    this.__pool.timeSpans[i].init(limitation);
            }
        }
        , __pool: {
            objects: {},
            timeSpans: {}
        }
    }
    function TimeSpan(limitation, isOwn) {

        this.init(limitation, isOwn);
        return this;
    }
    TimeSpan.prototype.getIsActive = function () {
        return this.timeBegin === this.timeEnd || this.timeEnd > Date.now();
    }
    TimeSpan.prototype.init = function (limitation, isOwn) {
        
        this.isOwn = isOwn;
        this.limitation = limitation;
        this.timeBegin = Date.now();
        this.timeEnd = this.timeBegin + limitation;
    }
    return {
        get: function (key) {
            return cachePool.get(key);
        },
        add: function (key, value, limitation) {
            return cachePool.add(key, value, limitation);
        },
        remove: function (key) {
            return cachePool.remove(key);
        },
        setLimitation: function (limitation, freshOwn) {
            return cachePool.setLimitation(limitation, freshOwn || false);//默认false
        }
    };
})(window);
/***************************************************************************************************************************************************************************************/
/*
 *  Extend
 */ 

